/************************************************************************************/
/*  Program name CAPAPIMP_AP_BANK_AC.sql                                            */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;
ALTER TRIGGER AP_BANK_ACCOUNTS_CT1 DISABLE;
ALTER TRIGGER AP_BANK_ACCOUNTS_CT2 DISABLE;


DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_AP_BANK_AC';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_BANK_ACCOUNTS;

	lv_Next_Seq_ID		NUMBER;
	lv_COA_ID			NUMBER;

BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_BANK_ACCOUNTS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_BANK_ACCOUNTS_MAP VALUES
			(
			C1.BANK_ACCOUNT_ID,
			C1.BANK_ACCOUNT_NUM,
			lv_Next_Seq_ID,
			C1.BANK_ACCOUNT_NUM
			);

			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAID(C1.SOB_SHORT_NAME);

			INSERT INTO AP_BANK_ACCOUNTS_ALL
			VALUES
			(
			lv_Next_Seq_ID,											--BANK_ACCOUNT_ID
			C1.BANK_ACCOUNT_NAME,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.BANK_ACCOUNT_NUM,
			C1.BANK_BRANCH_ID,
			CAPBRIMP_COMMON.GetSOBID(C1.SOB_SHORT_NAME),			--SET_OF_BOOKS_ID,
			C1.CURRENCY_CODE,
			C1.DESCRIPTION,
			C1.CONTACT_FIRST_NAME,
			C1.CONTACT_MIDDLE_NAME,
			C1.CONTACT_LAST_NAME,
			C1.CONTACT_PREFIX,
			C1.CONTACT_TITLE,
			C1.CONTACT_AREA_CODE,
			C1.CONTACT_PHONE,
			C1.MAX_CHECK_AMOUNT,
			C1.MIN_CHECK_AMOUNT,
			C1.ONE_SIGNATURE_MAX_FLAG,
			C1.INACTIVE_DATE,
			C1.AVG_FLOAT_DAYS,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.ASSET_CCID_D),			--ASSET_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.GAIN_CCID_D),			--GAIN_CODE_COMBINATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.LOSS_CCID_D),			--LOSS_CODE_COMBINATION_ID,
			C1.BANK_ACCOUNT_TYPE,
			C1.VALIDATION_NUMBER,
			C1.MAX_OUTLAY,
			C1.MULTI_CURRENCY_FLAG,
			C1.ACCOUNT_TYPE,
			C1.ATTRIBUTE_CATEGORY,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.POOLED_FLAG,
			C1.ZERO_AMOUNTS_ALLOWED,
			C1.REQUEST_ID,
			C1.PROGRAM_APPLICATION_ID,
			C1.PROGRAM_ID,
			C1.PROGRAM_UPDATE_DATE,
			C1.RECEIPT_MULTI_CURRENCY_FLAG,
			C1.CHECK_DIGITS,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),								--ORG_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.CASH_CLEARING_CCID_D),		--CASH_CLEARING_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.BANK_CHARGES_CCID_D),		--BANK_CHARGES_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.BANK_ERROR_CCID_D),			--BANK_ERRORS_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.EARNED_CCID_D),				--EARNED_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.UNEARNED_CCID_D),			--UNEARNED_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.ON_ACCOUNT_CCID_D),			--ON_ACCOUNT_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.UNAPPLIED_CCID_D),			--UNAPPLIED_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.UNIDENTIFIED_CCID_D),		--UNIDENTIFIED_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.FACTOR_CCID_D),				--FACTOR_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.RECEIPT_CLEARING_CCID_D),	--RECEIPT_CLEARING_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.REMITTANCE_CCID_D),			--REMITTANCE_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.SHORT_TERM_DEPOSIT_CCID_D),	--SHORT_TERM_DEPOSIT_CCID,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.BANK_ACCOUNT_NAME_ALT,
			C1.ACCOUNT_HOLDER_NAME,
			C1.ACCOUNT_HOLDER_NAME_ALT,
			C1.EFT_REQUESTER_ID,
			C1.EFT_USER_NUMBER,
			C1.PAYROLL_BANK_ACCOUNT_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.FUTURE_DATED_PAYMENT_CCID_D),	--FUTURE_DATED_PAYMENT_CCID,
			C1.EDISC_RECEIVABLES_TRX_ID,
			C1.UNEDISC_RECEIVABLES_TRX_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.BR_REMITTANCE_CCID_D),			--BR_REMITTANCE_CCID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.BR_FACTOR_CCID_D),				--BR_FACTOR_CCID,
			C1.BR_STD_RECEIVABLES_TRX_ID,
			C1.ALLOW_MULTI_ASSIGNMENTS_FLAG,
			C1.AGENCY_LOCATION_CODE,
			C1.IBAN_NUMBER
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER AP_BANK_ACCOUNTS_CT1 ENABLE;
ALTER TRIGGER AP_BANK_ACCOUNTS_CT2 ENABLE;
